package cn.xfdzx.android.apps.shop.fragment;

import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.adapter.HomeVPAdapter;
import cn.xfdzx.android.apps.shop.adapter.VideoVPAdapter;
import cn.xfdzx.android.apps.shop.evenbus.HomeTypeMessage;
import cn.xfdzx.android.apps.shop.evenbus.VideoListMessage;
import cn.xfdzx.android.apps.shop.fragment.type.TypeCommunityRetailFragment;
import cn.xfdzx.android.apps.shop.fragment.type.TypeMarketPurchaseFragment;
import cn.xfdzx.android.apps.shop.fragment.video.HotFragment;
import cn.xfdzx.android.apps.shop.fragment.video.MyFocusLiveFragment;
import cn.xfdzx.android.apps.shop.util.ProgressDialog;
import cn.xfdzx.android.apps.shop.video.VideoActivity;
import cn.xfdzx.android.apps.shop.base.BaseMvpFragment;
import cn.xfdzx.android.apps.shop.util.StatusBarUtil;

import static cn.xfdzx.android.apps.shop.activity.MainActivity.TYPE_IS_RETAIL;

public class ThreeFragment extends BaseMvpFragment {


    LinearLayout llState;
    TabLayout mTabLayout;
    ViewPager vpFragmentThree;

    private List<Fragment> mFragments;
    private String[] title = {"热门", "关注"};
    private RelativeLayout.LayoutParams item_bar;
    private VideoVPAdapter homeVPAdapter;

    @Override
    protected void initView(View view) {
        EventBus.getDefault().register(this);
        llState = view.findViewById(R.id.ll_state);
        mTabLayout = view.findViewById(R.id.tl_video_type);
        vpFragmentThree = view.findViewById(R.id.vp_fragment_three);

        initHigh();
        mFragments = new ArrayList<>();
        mFragments.add(HotFragment.newInstance(1));//热门
        mFragments.add(MyFocusLiveFragment.newInstance(1));//关注直播间
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        mTabLayout.setTabRippleColor(ColorStateList.valueOf(getContext().getResources().getColor(R.color.picture_color_transparent)));
        for (String tab : title) {
            mTabLayout.addTab(mTabLayout.newTab().setText(tab));
        }
        homeVPAdapter = new VideoVPAdapter(getChildFragmentManager(), title, mFragments, getActivity());
        mTabLayout.setTabRippleColor(ColorStateList.valueOf(getContext().getResources().getColor(R.color.picture_color_transparent)));
        vpFragmentThree.setAdapter(homeVPAdapter);
        mTabLayout.setupWithViewPager(vpFragmentThree);
        mTabLayout.addOnTabSelectedListener(new TabLayout.BaseOnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab.getPosition() == 0) {
                    EventBus.getDefault().post(new VideoListMessage(0));
                } else {
                    EventBus.getDefault().post(new VideoListMessage(1));
                }
                //选中了tab的逻辑
                View view = tab.getCustomView();
                TextView textView = view.findViewById(R.id.tv_title);
                TextView tv_bottom_bg = view.findViewById(R.id.tv_bottom_bg);
                textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                if (TYPE_IS_RETAIL == 0) {
                    textView.setTextColor(getResources().getColor(R.color.color_FB9129));
                    tv_bottom_bg.setBackgroundColor(getResources().getColor(R.color.color_FB9129));
                } else {
                    textView.setTextColor(getResources().getColor(R.color.color_02C557));
                    tv_bottom_bg.setBackgroundColor(getResources().getColor(R.color.color_02C557));
                }
                tv_bottom_bg.setVisibility(View.VISIBLE);
                textView.setTextSize(19);

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                //未选中tab的逻辑
                View view = tab.getCustomView();
                TextView textView = view.findViewById(R.id.tv_title);
                TextView tv_bottom_bg = view.findViewById(R.id.tv_bottom_bg);
                textView.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                textView.setTextColor(getResources().getColor(R.color.color_333333));
                tv_bottom_bg.setVisibility(View.GONE);
                textView.setTextSize(14);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        //设置自定义tab
        for (int i = 0; i < mTabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = mTabLayout.getTabAt(i);
            if (tab != null) {
                tab.setCustomView(homeVPAdapter.getTabView(i));
            }
        }
        View tab = mTabLayout.getTabAt(0).getCustomView();
        TextView textView = tab.findViewById(R.id.tv_title);
        TextView tv_bottom_bg = view.findViewById(R.id.tv_bottom_bg);
        textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        if (TYPE_IS_RETAIL == 0) {
            textView.setTextColor(getResources().getColor(R.color.color_FB9129));
            tv_bottom_bg.setBackgroundColor(getResources().getColor(R.color.color_FB9129));
        } else {
            textView.setTextColor(getResources().getColor(R.color.color_02C557));
            tv_bottom_bg.setBackgroundColor(getResources().getColor(R.color.color_02C557));
        }
        textView.setTextSize(19);
        tv_bottom_bg.setVisibility(View.VISIBLE);
        vpFragmentThree.setCurrentItem(0);// 设置当前显示标签页为第一页
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_three;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void XXX(HomeTypeMessage messageEvent) {
        View tab = mTabLayout.getTabAt(vpFragmentThree.getCurrentItem()).getCustomView();
        TextView textView = tab.findViewById(R.id.tv_title);
        TextView tv_bottom_bg = tab.findViewById(R.id.tv_bottom_bg);
        if (messageEvent.getMessage() == 0) {
            textView.setTextColor(getResources().getColor(R.color.color_FB9129));
            tv_bottom_bg.setBackgroundColor(getResources().getColor(R.color.color_FB9129));
        } else {
            textView.setTextColor(getResources().getColor(R.color.color_02C557));
            tv_bottom_bg.setBackgroundColor(getResources().getColor(R.color.color_02C557));
        }

    }
    private void initHigh() {
        item_bar = (RelativeLayout.LayoutParams) llState.getLayoutParams();
        item_bar.height = StatusBarUtil.getStatusBarHeight(getActivity());
        llState.setLayoutParams(item_bar);
    }

    @Override
    public void onError(Throwable throwable) {

    }

    public static ThreeFragment newInstance(int userUid) {
        Bundle bundle = new Bundle();
        ThreeFragment fragment = new ThreeFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onDestroyView() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {

        super.onDestroy();
    }
}
